package com.example.controller;

import com.example.domain.Schedule;
import com.example.service.ScheduleService;
import com.example.util.EasyuiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/schedule")
@Slf4j
public class ScheduleController {
    
    
    @Autowired
    private ScheduleService scheduleService;
    
    @RequestMapping(value = "/all")
    public List<Schedule> all() {
        return scheduleService.list();
    }
    
    /**
     * 查询所有学生,并分页
     *
     * @param page    页码,从1开始,后端从0开始
     * @param rows    每页的条数
     * @param sort    需要排序的列
     * @param order   排序的方向
     * @param clazzid 班级id,可以通过clazzid来获取这个班级的所有人
     * @return Easyui中datagrid的数据结构, row和data
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public EasyuiResult<Schedule> list(@RequestParam(name = "page", defaultValue = "1", required = false) int page,
                                       @RequestParam(name = "rows", defaultValue = "10", required = false) int rows,
                                       @RequestParam(name = "sort", defaultValue = "id", required = false) String sort,
                                       @RequestParam(name = "order", defaultValue = "DESC", required = false) String order,
                                       @RequestParam(name = "clazzid", defaultValue = "0", required = false) int clazzid,
                                       @RequestParam(name = "courseid", defaultValue = "0", required = false) int courseid,
                                       @RequestParam(name = "teacherid", defaultValue = "0", required = false) int teacherid
    ) {
        log.info("课程列表,参数为 page={},row={},sort={},order={},clazzid={},courseid={},teacherid={}",
                page, rows, sort, order, clazzid, courseid, teacherid);
        // int nowPage = 0;
        // if (page > 0)
        //     nowPage = page - 1;
        
        return scheduleService.list(page - 1, rows, sort, order, clazzid, courseid, teacherid);
    }
    
    /**
     * 根据id删除课程表的内容
     *
     * @param id 课程id
     * @return success:成功,error:失败
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(int id) {
        log.info("删除课程,id={}", id);
        scheduleService.delete(id);
        return "success";
    }
    
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(int clazzid, int courseid, int teacherid){
        log.info("添加或修改课程信息,clazzid={},courseid={},teacherid={}", clazzid,courseid,teacherid);
        return scheduleService.add(clazzid,courseid,teacherid);
    }
}
